package client.models;

/*
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 * ClientMap.java
 *
 * Steven Louie Apr. 8, 2012
 */
import client.ActinoClient;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;

public class ClientMap extends TiledMap {

    public ClientMap(String map, String dir) throws SlickException {
        super(map, dir);
    }

    public void renderMapWithoutWalkBehind(Graphics g, int px, int py) {
        g.scale(ActinoClient.SCALE_FACTOR, ActinoClient.SCALE_FACTOR);
        render((-px + 400) / ActinoClient.SCALE_FACTOR,
                (-py + 300) / ActinoClient.SCALE_FACTOR,
                getLayerIndex("Walkable"));
        render((-px + 400) / ActinoClient.SCALE_FACTOR,
                (-py + 300) / ActinoClient.SCALE_FACTOR,
                getLayerIndex("Collisions"));
        render((-px + 400) / ActinoClient.SCALE_FACTOR,
                (-py + 300) / ActinoClient.SCALE_FACTOR,
                getLayerIndex("Water"));
        render((-px + 400) / ActinoClient.SCALE_FACTOR,
                (-py + 300) / ActinoClient.SCALE_FACTOR,
                getLayerIndex("Interactable"));
        g.resetTransform();
    }

    public void renderWalkBehindMap(Graphics g, int px, int py) {
        g.scale(ActinoClient.SCALE_FACTOR, ActinoClient.SCALE_FACTOR);
        render((-px + 400) / ActinoClient.SCALE_FACTOR,
                (-py + 300) / ActinoClient.SCALE_FACTOR,
                getLayerIndex("WalkBehind"));
        g.resetTransform();
    }

    public boolean isColliding(ActinoPlayer player) {
        int tx = player.x, ty = player.y;
        int collision = 0;
        if (player.dir == Direction.UP) {
            ty--;
        } else if (player.dir == Direction.DOWN) {
            ty++;
        } else if (player.dir == Direction.RIGHT) {
            tx++;
        } else if (player.dir == Direction.LEFT) {
            tx--;
        }
        if (tx < 0 || ty < 0 || tx >= getWidth() || ty >= getHeight()) {
            return true;
        }
        collision += getTileId(tx, ty, getLayerIndex("Collisions"));
        collision += getTileId(tx, ty, getLayerIndex("Water"));
        if (collision == 0) {
            return false;
        }
        return true;
    }

    public boolean interact(ActinoPlayer player) {
        int tx = player.x, ty = player.y;
        if (player.dir == Direction.UP) {
            ty--;
        } else if (player.dir == Direction.DOWN) {
            ty++;
        } else if (player.dir == Direction.RIGHT) {
            tx++;
        } else if (player.dir == Direction.LEFT) {
            tx--;
        }
        int id;
        if (tx < 0 || ty < 0 || tx >= getWidth() || ty >= getHeight()
                || (id = getTileId(tx, ty, getLayerIndex("Interactable"))) == 0) {
            return false;
        }
        String npc = getTileProperty(id, "npc", "");
        if (!npc.isEmpty()) {
            ActinoClient.getInstance().getTcp().sendNPCTest(npc);
        }
        return true;
    }
}
